<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\BannerRepository;
use App\Http\Servers\Admin\BannerServer;
use App\Http\Servers\Admin\ImageServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BannerController extends CommonController
{
    //Banner标签列表
    public function label(Request $request, BannerServer $bannerServer)
    {
        if ($request->has('del')) {
            if ($message = $bannerServer->noPower()) {
                return $message;
            }

            if ($message = $bannerServer->hasBanner($request->del)) {
                return $message;
            }

            $bannerLabel = \App\BannerLabel::findorfail($request->del);
            $result = $bannerLabel->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', '删除Banner标签 ' . $bannerLabel->name . ' 成功!');
            } else {
                $message['message'] = message(false, '删除失败', '删除Banner标签 ' . $bannerLabel->name . ' 失败!');
            }
            return back()->with($message);
        }

        if ($request->has('change')) {
            if ($message = $bannerServer->noPower()) {
                return $message;
            }

            $bannerLabel = \App\BannerLabel::findorfail($request->id);
            $change = $request->change;

            if (array_search($change, ['status']) === false) {
                $message['message'] = message(false, '修改失败', 'Banner该属性无法修改!');
                return back()->with($message);
            }

            $bannerLabel->$change = (bool)$bannerLabel->$change ? 0 : 1;
            $result = $bannerLabel->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', '修改Banner标签 ' . $bannerLabel->name . ' 状态成功!');
            } else {
                $message['message'] = message(false, '修改失败', '修改Banner标签 ' . $bannerLabel->name . ' 状态失败!');
            }
            return back()->with($message);
        }

        $data['bannerLabels'] = \App\BannerLabel::get();

        return view('admin.banner.label', $data);
    }

    //Banner标签添加
    public function label_add(Request $request, BannerServer $bannerServer)
    {
        if ($request->isMethod('post')) {
            if ($message = $bannerServer->noPower()) {
                return $message;
            }

            $this->validate($request, [
                'name' => 'required',
                'path' => 'required|alpha|unique:banner_labels',
                'width' => 'required',
                'height' => 'required',
            ]);

            $create['name'] = $request->name;
            $create['path'] = $request->path;
            $create['width'] = $request->width;
            $create['height'] = $request->height;
            $create['status'] = $request->has('status') ? 1 : 0;

            $result = \App\BannerLabel::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', 'Banner标签 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', 'Banner标签 ' . $request->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        return view('admin.banner.label_add');
    }

    //Banner标签编辑
    public function label_edit(Request $request, BannerServer $bannerServer)
    {
        if ($request->isMethod('post')) {
            if ($message = $bannerServer->noPower()) {
                return $message;
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'path' => 'required|alpha|unique:banner_labels,path,' . $request->id . ',id',
                'width' => 'required',
                'height' => 'required',
            ]);

            $update['name'] = $request->name;
            $update['path'] = $request->path;
            $update['width'] = $request->width;
            $update['height'] = $request->height;
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\BannerLabel::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', 'Banner标签 ' . $request->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', 'Banner标签 ' . $request->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        $data['bannerLabel'] = \App\BannerLabel::findorfail($request->id);

        return view('admin.banner.label_edit', $data);
    }

    //Banner列表
    public function banner(Request $request, BannerRepository $bannerRepository, ImageServer $imageServer, BannerServer $bannerServer)
    {
        if ($message = $bannerServer->delBanner($request)) {
            return $message;
        }

        if ($message = $bannerServer->bannerStatusChange($request)) {
            return $message;
        }

        $data['banners'] = $bannerRepository->getBannerGroupLabel();

        return view('admin.banner.banner', $data);
    }

    //Banner图片添加
    public function banner_add(Request $request, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'label' => 'required',
                'image' => 'required',
                'xindex' => 'required',
                'url' => 'url',
                'tag' => 'required'
            ]);

            $image = $imageServer->uploadImage($request->image, 'banner', 'banner');
            if ($image) {
                $create['image'] = $image;
            } else {
                $message['message'] = message(false, 'Banner添加失败', 'Banner图片 上传失败!');
                return back()->with($message);
            }

            $create['label'] = $request->label;
            $create['xindex'] = $request->xindex;
            $create['url'] = $request->url;
            $create['tag'] = $request->tag;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['status'] = $request->has('status') ? 1 : 0;

            $result = \App\Banner::create($create);
            if ($result) {
                $message['message'] = message(true, 'Banner添加成功', 'Banner图片添加成功!');
            } else {
                $message['message'] = message(false, 'Banner添加失败', 'Banner图片添加失败!');
            }
            return back()->with($message);
        }
        $data['bannerLabel'] = \App\BannerLabel::findorfail($request->label);

        $tmpTag = explode(',', config('set.web_keywords'));
        shuffle($tmpTag);
        $data['tag'] = $tmpTag[0];

        return view('admin.banner.banner_add', $data);
    }

    //Banner图片编辑
    public function banner_edit(Request $request, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'id' => 'required',
                'xindex' => 'required',
                'url' => 'url',
                'tag' => 'required'
            ]);

            $banner = \App\Banner::findorfail($request->id);

            if ($request->hasFile('image')) {
                $image = $imageServer->uploadImage($request->image, 'banner', 'banner');
                if ($image) {
                    $update['image'] = $image;
                } else {
                    $message['message'] = message(false, 'Banner添加失败', 'Banner图片 上传失败!');
                    return back()->with($message);
                }
            }

            $update['xindex'] = $request->xindex;
            $update['url'] = $request->url;
            $update['tag'] = $request->tag;
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\Banner::where('id', $request->id)->update($update);
            if ($result) {
                if ($request->hasFile('image')) {
                    $imageServer->deleteImage($banner->image);
                }
                $message['message'] = message(true, 'Banner编辑成功', 'Banner图片编辑成功!');
            } else {
                $message['message'] = message(false, 'Banner编辑失败', 'Banner图片编辑失败!');
            }
            return back()->with($message);
        }

        $data['banner'] = \App\Banner::join('images', 'images.id', '=', 'banners.image')
            ->select('banners.*', 'images.path as image')
            ->findorfail($request->id);

        $data['bannerLabel'] = \App\BannerLabel::findorfail($data['banner']->label);

        return view('admin.banner.banner_edit', $data);
    }
}
